home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2489 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer Conversion
  5. Date: Sun, 21 Jan 96 19:16:16 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <822251776snz@genesis.demon.co.uk>
  9. References: <4ds4jq$fo4@su3.in.net> <20JAN199622465412@erich.triumf.ca>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <20JAN199622465412@erich.triumf.ca>
  16.            bennett@erich.triumf.ca "P.Bennett" writes:
  17.  
  18. >In article <4ds4jq$fo4@su3.in.net>, poundss@in.net (Sam Pounds) writes...
  19. >>I am having a problem with a "string concatenate" function.
  20. >>When I compile my little program it works, but I get a
  21. >>"suspicious pointer" conversion warning. The function is
  22. >>below and I call it with two strings that I want to concatenate.
  23. >> 
  24. >>char *my_strcat(const char *a, const char *b)
  25. >>{
  26. >>    char done[1024];
  27. >>    char *p = done;
  28. >> 
  29. >>    while (*a)
  30. >>      *p++ = *a++;
  31. >>    while (*b)
  32. >>      *p++ = *b++;
  33. >>    *p = '\0';
  34. >>    return done; /* this is the suspicious pointer conversion error */
  35. >>}
  36. >
  37. >Your function is declared to return a char pointer, and you are returning an
  38. >array, which is not the same thing.  Sometimes char array names act like char
  39. >pointers (particularly when used as function parameters), but not here.
  40.  
  41. In this case done *does* evaluate to a pointer to the first element of the
  42. array. I don't know why the compiler would diagnose "suspicious pointer
  43. conversion" here since no pointer conversion is taking place.
  44.  
  45. >A more important problem here is that "done" is an automatic array which will
  46. >cease to exist when the function returns, possibly being over-written on the
  47. >next call to another function.
  48.  
  49. Yes, that is the obvious problem in the code. Maybe the compiler was trying
  50. to warn about this.
  51.  
  52. >You could declare "done" as a char pointer, and malloc() some memory for it to
  53. >point to - this would solve both problems, but the calling function would have
  54. >to free() that malloc()ed memory sometime.
  55. >
  56. >You could also declare done as a static array (just add "static" before the
  57. >present declaration), then the array will exist for the life of the program.
  58. >I _think_ (and I know someone will correct me if I'm wrong) that changeing
  59. >"return done;" to "return &done;" will fix the "suspicious pointer conversion"
  60. >error.
  61.  
  62. return &done would result in an illegal pointer conversion i.e. converting
  63. a char (*)[1024] to a char *.
  64.  
  65. Usually the best approach in this sort of situation is to have the array
  66. allocated in the caller and pass pointer to it as an extra argument, instead
  67. of returning a pointer.
  68.  
  69. -- 
  70. -----------------------------------------
  71. Lawrence Kirby | fred@genesis.demon.co.uk
  72. Wilts, England | 70734.126@compuserve.com
  73. -----------------------------------------
  74.